﻿using OpenTK.Mathematics;

namespace TrinityEngine
{
    public class Sprite : IDisposable
    {
        public readonly Texture texture;

        public readonly int pixelPerUnit;

        public readonly Rect region;

        private bool _disposed = false;

        public Matrix4 imageToUnitMatrix => Matrix4.CreateScale(texture.width / pixelPerUnit, texture.height / pixelPerUnit, 1.0f);


        public Vector2[] texCoords => new Vector2[4] {
            region.bottomLeft / texture.size,
            region.bottomRight / texture.size,
            region.topRight / texture.size,
            region.topLeft / texture.size,
        };

        public Sprite(Texture texture, Rect region, int pixelPerUnit)
        {
            this.texture = texture;
            this.region = region;
            this.pixelPerUnit = pixelPerUnit;
        }

        ~Sprite()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool diposing)
        {
            if (_disposed)
            {
                return;
            }

            if (diposing)
            {

            }

            texture.Dispose();

            _disposed = true;
        }
    }
}
